package kr.hkit.masan_alarm;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DB_Manager {
	private final Context context;
	static final String DB = "masan_alarm";
	static final String TABLE_ALARM = "alarm";

	private SQLiteDatabase db;
	private NoteOpenHelper dbHelper;
	// ////////////////////////////////////////////////////////////////
	static final int DB_VERSION = 1;

	// alarm table
	public static final String ALARM_ON = "onoff";
	public static final String ALARM_APDAY = "apday";
	public static final String ALARM_HOUR = "hour";
	public static final String ALARM_MINUTE = "minute";
	public static final String ALARM_VIBRATE = "vibrate";
	public static final String ALARM_RINGTONE = "ring";

	// /////////////////////////////////////////////////////////////////
	static final String CREATE_ALARM = "create table " + TABLE_ALARM
			+ " (_id integer primary key autoincrement, " + ALARM_ON
			+ " integer, " + ALARM_APDAY + " integer, " + ALARM_HOUR
			+ " integer, " + ALARM_MINUTE + " integer, " + ALARM_VIBRATE
			+ " integer, " + ALARM_RINGTONE + " text);";
	
	static final String INSERT_ALARM = "insert into " + TABLE_ALARM 
			+ " values (null, '', '', '', '', '', '');";
	
	static final String DROP = "drop table if exists " + TABLE_ALARM;
	// /////////////////////////////////////////////////////////////////
	
	public DB_Manager(Context ctx) {
		context = ctx;
	}//const

	
	private static class NoteOpenHelper extends SQLiteOpenHelper {
		public NoteOpenHelper(Context c) {
			super(c, DB, null, DB_VERSION);
		}//const

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(CREATE_ALARM);
			db.execSQL(INSERT_ALARM);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
			db.execSQL(DROP);
			onCreate(db);
		}
	}//NoteOpenHelper

	
	public DB_Manager open() throws SQLException {
		dbHelper = new NoteOpenHelper(context);
		db = dbHelper.getWritableDatabase();
		return this;
	}//open

	
	public void close() {
		dbHelper.close();
	}//close

	
	public Cursor fetchAllAlarm() {
		return db.query(TABLE_ALARM, null, null, null, null, null, ALARM_HOUR
				+ " asc, " + ALARM_MINUTE + " asc");
	}//fetchAllAlarm

	
	public String addAlarm(int on, int day, int hour, int min, int vib,
			String ring) {
		ContentValues values = new ContentValues();
		values.put(ALARM_ON, on);
		values.put(ALARM_APDAY, day);
		values.put(ALARM_HOUR, hour);
		values.put(ALARM_MINUTE, min);
		values.put(ALARM_RINGTONE, ring);
		values.put(ALARM_VIBRATE, vib);

		long id = db.insert(TABLE_ALARM, null, values);
		if (id < 0) {
			return "";
		}
		return Long.toString(id);
	}//addAlarm

	// ////////////////////////////////////////////////////////////////////////////////////////
	public void delAlarm(String id) {
		db.delete(TABLE_ALARM, "_id = ?", new String[] { id });
	}//delAlarm

	
	public void modifyAlarm(long id, int on, int day, int hour, int min,
			int vib, String ring) {
		ContentValues values = new ContentValues();
		values.put(ALARM_ON, on);
		values.put(ALARM_APDAY, day);
		values.put(ALARM_HOUR, hour);
		values.put(ALARM_MINUTE, min);
		values.put(ALARM_RINGTONE, ring);
		values.put(ALARM_VIBRATE, vib);

		db.update(TABLE_ALARM, values, "_id" + "='" + id + "'", null);
	}//modifyAlarm

	public void modifyAlarmOn(long id, int on) {
		ContentValues values = new ContentValues();
		values.put(ALARM_ON, on);

		db.update(TABLE_ALARM, values, "_id" + "='" + id + "'", null);
	}//modifyAlarmOn

}
